import tinymce from "tinymce/tinymce";
import Editor from "@tinymce/tinymce-vue";
import "tinymce/themes/silver";
import "tinymce/icons/default";
// 编辑器插件plugins
import "tinymce/plugins/image"; // 插入上传图片插件
import "tinymce/plugins/media"; // 插入视频插件
import "tinymce/plugins/table"; // 插入表格插件
import "tinymce/plugins/lists"; // 列表插件
import "tinymce/plugins/wordcount"; // 字数统计插件
import "@/assets/lineheight/plugin";
import "tinymce/plugins/advlist";
import "tinymce/plugins/paste";
import "tinymce/plugins/preview";
import "tinymce/plugins/fullscreen";
import {uploadFile, uploadRemove, uploadEditUploadImg} from "@/api/upload/upload";
export const editorMixin = {
    components: {
        Editor,
        tinymce,
      },
    data() {
        let  _this = this;
        return {
            initEditor: {
                language_url: `/tinymce/langs/zh_CN.js`,
                language: "zh_CN",
                skin_url: `/tinymce/skins/ui/oxide`,
                content_css: `/tinymce/skins/content/default/content.css`,
                height: 500,
                width: '100%',
                font_formats:
                    "微软雅黑='微软雅黑';宋体='宋体';黑体='黑体';仿宋='仿宋';楷体='楷体';隶书='隶书';幼圆='幼圆';Andale Mono=andale mono,times;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;Comic Sans MS=comic sans ms,sans-serif;Courier New=courier new,courier;Georgia=georgia,palatino;Helvetica=helvetica;Impact=impact,chicago;Symbol=symbol;Tahoma=tahoma,arial,helvetica,sans-serif;Terminal=terminal,monaco;Times New Roman=times new roman,times;Trebuchet MS=trebuchet ms,geneva;Verdana=verdana,geneva;Webdings=webdings;Wingdings=wingdings",
                plugins: "advlist   paste preview fullscreen   lists image table wordcount ",
                toolbar: "undo redo fontselect fontsizeselect  formatselect  bold italic underline strikethrough forecolor backcolor lineheight  alignleft aligncenter alignright alignjustify   bullist numlist outdent indent linheight lists image table  removeformat",
                branding: false,
                menubar: false,
                selector: "textarea",
                file_browser_callback_types: "file image media",
                images_upload_handler: function (blobInfo, succFun, failFun) {
                  uploadEditUploadImg(blobInfo.blob(), "editor_img").then((data) => {
                    if (data.code === 200) {
                      succFun(_this.imageUrlprefix + data.data.url);
                    }
                  });
                },
              },
        }
  },
  methods: {
    beforeCoverUpload(file) {
      const isJPG = file.type === "image/jpeg" || file.type === "image/jpg" || file.type === "image/png";
      const isLt5M = file.size / 1024 / 1024 < 5;
      if (!isJPG) {
        this.$message.error("上传图片只能是 JPG JPEG PNG 格式!");
      }
      if (!isLt5M) {
        this.$message.error("上传图片大小不能超过 5MB!");
      }
      return isJPG && isLt5M;
    },
   async fileUpload(file, url,type='coverImg') {
      if (url) {
        await uploadRemove({url})
      }
      const formData = new FormData();
      formData.append("file", file);
      formData.append("type", type);
       return uploadFile(formData).then((res) => {
         return res;
      });
    }
  }
}